public class Practice1 {
    //此题是二维背包种的01背包,注意其中的初始化与填表顺序即可
    //https://leetcode.cn/problems/ones-and-zeroes/
    //首先的解法
    public int getOne(String s){
        int count = 0;
        for(int i = 0;i < s.length();i++){
            if(s.charAt(i) == '1'){
                count++;
            }
        }
        return count;
    }

    public int findMaxForm(String[] strs, int m, int n) {
        //前i个元素种, 满足j个0和k个1的最大子集的长度
        int len = strs.length;
        int[][][] dp = new int[len + 1][m + 1][n + 1];
        for(int i = 1;i <= len;i++){
            int one = getOne(strs[i - 1]);
            int zone = strs[i - 1].length() - one;
            for(int j = 0;j <= m;j++){
                for(int k = 0;k <= n;k++){
                    dp[i][j][k] = dp[i - 1][j][k];
                    if(j - zone >= 0 && k - one >= 0){
                        dp[i][j][k] = Math.max(dp[i][j][k],dp[i - 1][j - zone][k - one] + 1);
                    }
                }
            }
        }
        return dp[len][m][n];
    }







    //空间优化
    public int getOne(String s){
        int count = 0;
        for(int i = 0;i < s.length();i++){
            if(s.charAt(i) == '1'){
                count++;
            }
        }
        return count;
    }

    public int findMaxForm(String[] strs, int m, int n) {
        //前i个元素种, 满足j个0和k个1的最大子集的长度
        int len = strs.length;
        int[][] dp = new int[m + 1][n + 1];
        for(int i = 1;i <= len;i++){
            int one = getOne(strs[i - 1]);
            int zone = strs[i - 1].length() - one;
            for(int j = m;j >= zone;j--){
                for(int k = n;k >= one;k--){
                    dp[j][k] = Math.max(dp[j][k],dp[j - zone][k - one] + 1);
                }
            }
        }
        return dp[m][n];
    }
    public static void main(String[] args) {

    }
}
